package com.example.base.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.base.adapter.BannerAdapter;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;

public class BannerViewPager extends ViewPager {
  private BannerAdapter mAdapter;
  private final int SCROLL_MSG = 0x0011;
  private int mCutDowntIME = 2000;
  private BannerScoller mScroller;
  private final String TAG = "BannerViewPager";
  @SuppressLint("HandlerLeak")
  private Handler mHandle = new Handler() {
    @Override
    public void handleMessage(@NonNull Message msg) {
      setCurrentItem(getCurrentItem() + 1);
      startRoll();
    }
  };


  public BannerViewPager(@NonNull @NotNull Context context) {
    this(context, null);
  }

  public BannerViewPager(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
    super(context, attrs);
    try {
      Field field = ViewPager.class.getDeclaredField("mScroller");
      mScroller = new BannerScoller(context);
      field.setAccessible(true);
      try {
        field.set(this, mScroller);
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    }

  }

  public void setScrollerDuration(int scrollerDuration) {
    mScroller.setmScrollerDuration(scrollerDuration);
  }

  public void setAdapter(@Nullable @org.jetbrains.annotations.Nullable BannerAdapter adapter) {
    this.mAdapter = adapter;
    setAdapter(new BannerPagerAdapter());
  }

  public void startRoll() {
    mHandle.removeMessages(SCROLL_MSG);
    mHandle.sendEmptyMessageDelayed(SCROLL_MSG, mCutDowntIME);

  }

  @Override
  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    mHandle.removeMessages(SCROLL_MSG);
    mHandle = null;
  }

  //给viewpager设置适配器
  private class BannerPagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
      //实现无限循环
      return 1000;
    }

    @Override
    public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull java.lang.Object object) {
      return view == object;
    }

    //创建viewpager条目回调的方法
    @NonNull
    @Override
    public java.lang.Object instantiateItem(@NonNull ViewGroup container, int position) {
      Log.d(TAG,"position  == "+position );
      Log.d(TAG,"position % 4  == "+(position % 4));
      View bannerItemView = mAdapter.getView(position % 3);
      if (bannerItemView!=null){
        container.addView(bannerItemView);
      }
      return bannerItemView;
    }


    //销毁viewpager条目的回调方法
    @Override
    public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull java.lang.Object object) {
      container.removeView((View) object);
      object = null;
    }
  }
}
